﻿using FreeSql;
using FreeSql.DataAnnotations;
using Microsoft.Extensions.Options;
using System.Reflection;

 namespace FreeSqlMySqlExtensions
{
    public class SeedService
    {
        private readonly IFreeSql _fsql;
        private readonly DbConfig _dbConfig;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fsql"></param>
        /// <param name="dbConfig"></param>
        public SeedService(IFreeSql fsql, IOptions<DbConfig> dbConfig)
        {
            _fsql = fsql;
            _dbConfig = dbConfig.Value;
        }




        /// <summary>
        /// 同步分表 (定时调用，按数据池) 每月月初执行，月中执行，月末执行。
        /// </summary>
        /// <returns></returns>
        public IResultDTO SynTable(Assembly[] assemblies)
        {
            GetTypesByTableAttribute(assemblies, out Type[] ts, out Type[] ms);
            if (ts != null && ts.Length > 0)
            {
                foreach (var item in ts)
                {
                    try
                    {
                        _fsql.CreateTableAddMonth(item);
                    }
                    catch (Exception ex)
                    {

                        throw new Exception($"{item.Name}同步结果失败：{ex.Message}");
                    }

                }
            }
            return ResultDTO.Ok();
        }


        /// <summary>
        /// 获得指定程序集表实体获取enties 
        /// </summary>
        /// <param name="atable">自动分表(无临时表)</param>
        /// <param name="mtable">手工分表（有临时表）</param>
        /// <returns></returns>
        private Type[] GetTypesByTableAttribute(Assembly[] assemblies, out Type[] atable, out Type[] mtable)
        {
            List<Type> tableAssembies = new List<Type>();
            var ts = new List<Type>();
            var ms = new List<Type>();
            foreach (var assembly in assemblies)
            {
                foreach (Type type in assembly.GetExportedTypes())
                {
                    foreach (Attribute attribute in type.GetCustomAttributes(false))
                    {
                        if (attribute is TableAttribute tableAttribute)
                        {
                            if (tableAttribute.DisableSyncStructure == false)
                            {
                                if (tableAttribute.AsTable != null)
                                {
                                    ts.Add(type);
                                }
                                else
                                {
                                    if (type.GetCustomAttribute(typeof(SplitTable), false) is SplitTable tdb)
                                    {
                                        if (!string.IsNullOrWhiteSpace(tdb.AsTable))
                                        {
                                            ms.Add(type);
                                        }
                                    }
                                    tableAssembies.Add(type);
                                }
                            }
                        }

                    }
                };
            }
            atable = ts.ToArray();
            mtable = ms.ToArray();
            return tableAssembies.ToArray();
        }


        private void HasData<T>(IFreeSql _fsql, IEnumerable<T> data)
        {
            if (data.Any() == false) return;
            var sdCopy = data.Select(a => (object)a).ToList();
            var sdCopyLock = new object();

            object[] sd = null;
            lock (sdCopyLock)
                sd = sdCopy?.ToArray();
            if (sd == null || sd.Any() == false) return;
            var et = typeof(T);
            if (_fsql.Select<object>().AsType(et).Any()) return;

            var repo = _fsql.GetRepository<object>();
            repo.DbContextOptions.EnableCascadeSave = true;
            repo.DbContextOptions.NoneParameter = true;
            repo.AsType(et);
            repo.Insert(sd);
            lock (sdCopyLock)
                sdCopy = null;
            return;
        }

        /// <summary>
        /// 主库种子数据
        /// </summary>
        public void Seed(Assembly[] assemblies)
        {
            if (_dbConfig.SyncStructure)
            {
                var types = GetTypesByTableAttribute(assemblies, out Type[] ts, out Type[] ms);
                _fsql.CodeFirst.SyncStructure(types);
            }
            if (_dbConfig.SeedData)
            {
               /* SysOrg adminOrg = new SysOrg()
                {
                    Id = YitIdHelper.NextId(),
                    Name = $"默认部门",
                    Code = "001",
                    Mark = 2,
                    Description = "系统初始部门",
                };
                HasData<SysOrg>(_fsql, new[] { adminOrg });

                SysUser sysUser = new SysUser
                {
                    Id = YitIdHelper.NextId(),
                    UserName = "admin",
                    Password = MD5Encrypt.Encrypt32("111111"),
                    DisplayName = "超级管理员",
                    No = "0001",
                    OrgId = adminOrg.Id,
                    Mobile = "11111111111",
                    UserType = Core.Entity.Enums.UserType.SuperAdmin,
                    CreatedByName = "INSTALL"
                };

                _fsql.HasData(new List<SysUser>() { sysUser });


                SysRole sysRole = new SysRole
                {
                    Id = YitIdHelper.NextId(),
                    Name = "超级管理员",
                    Code = "SuperAdmin",
                    Condition = JSON.Serialize(new ConditionDTO { DataType = 1 }),
                    Home = 1,
                    Description = "超级管理员，不能删除"
                };
                _fsql.HasData(new List<SysRole>() { sysRole });

                SysRUserRole sysRUserRole = new SysRUserRole
                {
                    Id = YitIdHelper.NextId(),
                    RoleId = sysRole.Id,
                    UserId = sysUser.Id,
                };
                _fsql.HasData(new List<SysRUserRole>() { sysRUserRole });

                var res = JSON.Deserialize<List<SysResource>>(rs);
                _fsql.HasData(res);

                var rrs = res.Select(x => new SysRRoleResource
                {
                    RoleId = sysRole.Id,
                    ResourceId = x.Id,
                }).ToList();
                _fsql.HasData(rrs);*/
            }

        }



        /// <summary>
        /// 平台菜单
        /// </summary>
        string rs = @"
	[
		{
			""id"": 5130743296163909,
			""parentId"": null,
			""name"": ""home"",
			""path"": ""/home"",
			""component"": """",
			""redirect"": null,
			""title"": ""个人中心"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-eleme-filled"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": 0,
			""orderIndex"": 3,
			""isLeaf"": 0,
			""treePath"": ""5130743296163909"",
			""treeText"": null,
			""revision"": 10,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5130744335171653,
			""parentId"": null,
			""name"": ""setting"",
			""path"": ""/setting"",
			""component"": """",
			""redirect"": null,
			""title"": ""系统配置"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-setting"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": 0,
			""orderIndex"": 11,
			""isLeaf"": 0,
			""treePath"": ""5130744335171653"",
			""treeText"": null,
			""revision"": 11,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": 6343207866335301,
			""updated_by_name"": ""admin"",
			""updated_time"": ""2023-07-05 17:51:27.489""
		},
		{
			""id"": 5130753388183621,
			""parentId"": 5130743296163909,
			""name"": ""dashboard"",
			""path"": ""/dashboard"",
			""component"": ""home"",
			""redirect"": null,
			""title"": ""控制台"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-menu"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 5,
			""isLeaf"": 1,
			""treePath"": ""5130743296163909>5130753388183621"",
			""treeText"": null,
			""revision"": 1,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5130757572657221,
			""parentId"": 5130743296163909,
			""name"": ""userCenter"",
			""path"": ""/usercenter"",
			""component"": ""userCenter"",
			""redirect"": null,
			""title"": ""帐号信息"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-user-filled"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 6,
			""isLeaf"": 1,
			""treePath"": ""5130743296163909>5130757572657221"",
			""treeText"": null,
			""revision"": 3,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5130767783755845,
			""parentId"": 5149609718382661,
			""name"": ""settingMenu"",
			""path"": ""/setting/menu"",
			""component"": ""setting/menu"",
			""redirect"": null,
			""title"": ""菜单管理"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-fold"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 18,
			""isLeaf"": 1,
			""treePath"": ""5149609718382661>5130767783755845"",
			""treeText"": null,
			""revision"": 6,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5130778826244165,
			""parentId"": 5130744335171653,
			""name"": ""dept"",
			""path"": ""/setting/dept"",
			""component"": ""setting/dept"",
			""redirect"": null,
			""title"": ""部门管理"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""sc-icon-organization"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 12,
			""isLeaf"": 1,
			""treePath"": ""5130744335171653>5130778826244165"",
			""treeText"": null,
			""revision"": 8,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5130783116820549,
			""parentId"": 5149638447398981,
			""name"": ""role"",
			""path"": ""/setting/role"",
			""component"": ""setting/role"",
			""redirect"": null,
			""title"": ""角色管理"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-notebook"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": 0,
			""orderIndex"": 9,
			""isLeaf"": 1,
			""treePath"": ""5149638447398981>5130783116820549"",
			""treeText"": null,
			""revision"": 11,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5144569380405317,
			""parentId"": 5130743296163909,
			""name"": ""message"",
			""path"": ""/setting/message"",
			""component"": ""setting/message"",
			""redirect"": null,
			""title"": ""消息中心"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-message"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 7,
			""isLeaf"": 0,
			""treePath"": ""5130743296163909>5144569380405317"",
			""treeText"": null,
			""revision"": 5,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149493285748805,
			""parentId"": 5130744335171653,
			""name"": ""system"",
			""path"": ""/setting/system"",
			""component"": ""setting/system"",
			""redirect"": null,
			""title"": ""系统设置"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-tools"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 13,
			""isLeaf"": 1,
			""treePath"": ""5130744335171653>5149493285748805"",
			""treeText"": null,
			""revision"": 3,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149496800182341,
			""parentId"": 5149638447398981,
			""name"": ""user"",
			""path"": ""/setting/user"",
			""component"": ""setting/user"",
			""redirect"": null,
			""title"": ""用户管理"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-user-filled"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 10,
			""isLeaf"": 0,
			""treePath"": ""5149638447398981>5149496800182341"",
			""treeText"": null,
			""revision"": 11,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149501667409989,
			""parentId"": 5149609718382661,
			""name"": ""api"",
			""path"": ""/setting/api"",
			""component"": ""setting/api"",
			""redirect"": null,
			""title"": ""api管理"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-ice-cream-round"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 19,
			""isLeaf"": 1,
			""treePath"": ""5149609718382661>5149501667409989"",
			""treeText"": null,
			""revision"": 3,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149507850600517,
			""parentId"": 5130744335171653,
			""name"": ""dic"",
			""path"": ""/setting/dic"",
			""component"": ""setting/dic"",
			""redirect"": null,
			""title"": ""数据字典"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-document"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 14,
			""isLeaf"": 1,
			""treePath"": ""5130744335171653>5149507850600517"",
			""treeText"": null,
			""revision"": 4,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149515610456133,
			""parentId"": 5149609718382661,
			""name"": ""client"",
			""path"": ""/setting/client"",
			""component"": ""setting/client"",
			""redirect"": null,
			""title"": ""应用管理"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-help-filled"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 20,
			""isLeaf"": 1,
			""treePath"": ""5149609718382661>5149515610456133"",
			""treeText"": null,
			""revision"": 3,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149518968913989,
			""parentId"": 5130744335171653,
			""name"": ""syslog"",
			""path"": ""/setting/log"",
			""component"": ""setting/log"",
			""redirect"": null,
			""title"": ""系统日志"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-warning"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 15,
			""isLeaf"": 1,
			""treePath"": ""5130744335171653>5149518968913989"",
			""treeText"": null,
			""revision"": 4,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149518968913990,
			""parentId"": 5130744335171653,
			""name"": ""httplog"",
			""path"": ""/setting/httplog"",
			""component"": ""setting/httplog"",
			""redirect"": null,
			""title"": ""访问日志"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-clock"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 16,
			""isLeaf"": 1,
			""treePath"": ""5130744335171653>5149518968913989"",
			""treeText"": null,
			""revision"": 4,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149609718382661,
			""parentId"": null,
			""name"": ""reso"",
			""path"": ""/reso"",
			""component"": """",
			""redirect"": null,
			""title"": ""资源管理"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-film"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 17,
			""isLeaf"": 0,
			""treePath"": ""5149609718382661"",
			""treeText"": null,
			""revision"": 14,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5149638447398981,
			""parentId"": null,
			""name"": ""ac"",
			""path"": ""/ac"",
			""component"": """",
			""redirect"": null,
			""title"": ""帐号权限"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-reading-lamp"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": 0,
			""orderIndex"": 8,
			""isLeaf"": 0,
			""treePath"": ""5149638447398981"",
			""treeText"": null,
			""revision"": 11,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		},
		{
			""id"": 5844744185184325,
			""parentId"": 5149609718382661,
			""name"": ""fileselect"",
			""path"": ""/setting/file"",
			""component"": ""setting/file"",
			""redirect"": null,
			""title"": ""文件管理"",
			""hidden"": 0,
			""type"": ""menu"",
			""icon"": ""el-icon-files"",
			""affix"": null,
			""hiddenBreadcrumb"": null,
			""active"": null,
			""color"": null,
			""fullpage"": null,
			""orderIndex"": 21,
			""isLeaf"": 1,
			""treePath"": ""5149609718382661>5844744185184325"",
			""treeText"": null,
			""revision"": 2,
			""isDeleted"": 0,
			""isDisabled"": 0,
			""created_by"": 0,
			""created_by_name"": """",
			""created_time"": ""2023-03-16 16:21:13.550"",
			""updated_by"": null,
			""updated_by_name"": null,
			""updated_time"": null
		}
	]
";
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IResultDTO CreateAllSplitTable(Assembly[] assemblies)
        {
            return CreateAllSplitTable(assemblies, DateTime.Now.AddMonths(1));
        }

        /// <summary>
        /// 预热
        /// </summary>
        /// <returns></returns>
        public IResultDTO PreHot(Assembly[] assemblies)
        {
            var types = GetTypesByTableAttribute(assemblies, out Type[] ts, out Type[] ms);
            foreach (var type in types)
                _fsql.Select<object>().AsType(type);
            return ResultDTO.Ok();
        }



        /// <summary>
        /// 创建指定月分表
        /// </summary>
        /// <returns></returns>
        public IResultDTO CreateAllSplitTable(Assembly[] assemblies, DateTime? date)
        {

            CreateSplitTable(assemblies, date);

            return ResultDTO.Ok();
        }
        /// <summary>
        /// 创建分表(程序启动后创建一次，每周随机一次创建下月的分表)
        /// </summary>
        public void CreateSplitTable(Assembly[] assemblies, DateTime? date)
        {

            GetTypesByTableAttribute(assemblies, out Type[] ts, out Type[] ms);

            foreach (var item in ms)
            {
                var tb = _fsql.CodeFirst.GetTableByEntity(item);
                var begDate = _dbConfig.BegDate;
                while (true)
                {
                    _fsql.CodeFirst.SyncStructure(item, $"{tb.DbName}_{begDate.ToString("yyyyMM")}");
                    begDate = begDate.AddMonths(1);
                    if (begDate > DateTime.Now) break;
                }
            }

            if (ts != null && ts.Length > 0)
            {
                foreach (var item in ts)
                {
                    try
                    {
                        var begDate = _dbConfig.BegDate;
                        if (date.HasValue) begDate = date.Value.Date;
                        while (true)
                        {
                            _fsql.CreateTable(item, begDate, true);
                            begDate = begDate.AddMonths(1);
                            if (begDate > DateTime.Now) break;
                        }
                    }
                    catch (Exception ex)
                    {

                        throw new Exception($"{item.Name}同步结果失败：{ex.Message}");
                    }

                }
            }
        }


        /// <summary>
        /// 定时任务创建按月分表
        /// </summary>
        public void CreateNextTabe(Assembly[] assemblies, DateTime? month)
        {

            if (!month.HasValue) month = DateTime.Now.AddMonths(1);

            var types = GetTypesByTableAttribute(assemblies, out Type[] ts, out Type[] ms);

            foreach (var item in ms)
            {
                var tb = _fsql.CodeFirst.GetTableByEntity(item);

                _fsql.CodeFirst.SyncStructure(item, $"{tb.DbName}_{month.GetValueOrDefault().ToString("yyyyMM")}");
            }

            _fsql.CodeFirst.SyncStructure(types);

            if (ts != null && ts.Length > 0)
            {
                foreach (var item in ts)
                {
                    try
                    {
                        _fsql.CreateTable(item, month.GetValueOrDefault(), true);
                    }
                    catch (Exception ex)
                    {

                        throw new Exception($"{item.Name}同步结果失败：{ex.Message}");
                    }

                }
            }

        }

    }
}
